package org.webdb.webapp.jackson;

import java.io.IOException;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.SerializerProvider;
import org.postgis.Point;

public class PointJSONSerializer {

    public void serialize(Point point, JsonGenerator jgen,
            SerializerProvider provider) throws JsonGenerationException,
            IOException {
        jgen.writeStartObject();
        jgen.writeStringField(GEOJSON.TYPE_ATTRIBUTE, GEOJSON.POINT_TYPE_NAME);
        jgen.writeFieldName(GEOJSON.COORDINATES_ATTRIBUTE);
        writePoint(point, jgen);
        jgen.writeEndObject();
    }

    public void writePoint(Point point, JsonGenerator jgen)
            throws JsonGenerationException, IOException {
        // TODO Auto-generated method stub
        jgen.writeStartArray();
        jgen.writeNumber(point.getX());
        jgen.writeNumber(point.getY());
        if (point.getDimension() > 2)
            jgen.writeNumber(point.getZ());
        if (point.isMeasured())
            jgen.writeNumber(point.getM());
        jgen.writeEndArray();
    }

}
